package com.psq.business.login.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.psq.base.user.manager.UserManager
import com.psq.base.user.model.User
import com.psq.business.login.repository.LoginRepository
import com.psq.core.common.model.ApiResponse
import com.psq.core.common.model.BaseViewModel
import com.psq.core.common.provider.IMainProvider
import com.psq.core.common.provider.ROUTER_MAIN_PROVIDER
import com.psq.core.common.utils.tryLaunch
import javax.inject.Inject

class LoginViewModel  @Inject constructor(private val repository: LoginRepository) :
    BaseViewModel() {

    private val _loginLiveData = MutableLiveData<ApiResponse<User>>()
    val loginLiveData: LiveData<ApiResponse<User>> = _loginLiveData

    override fun start() {

    }

    // 手机号登录
    fun loginByPassword(phone: String, password: String) {
        tryLaunch({
            val params = mutableMapOf<String, Any>(
                "phone" to phone,
                "password" to password
            )
            _loginLiveData.value = repository.loginByPassword(params)
        })
    }




    // 跳转首页
    fun navMain() {
        val provider =
            ARouter.getInstance().build(ROUTER_MAIN_PROVIDER).navigation() as IMainProvider
        provider.navHome()
    }


    fun login(user: User?) {
        user?.let { UserManager.getInstance().login(it) }
    }


}